<?php
function rmdirr($dirname) {
	if(!file_exists($dirname)) {
		return false;
	}
	
	// Simple delete of a file
	if (is_file($dirname) || is_link($dirname)) {
		return unlink($dirname);
	}
	
	// Loop through the folders
	$dir = dir($dirname);
	while(false !== $entry = $dir->read()) {
		// Skip Pointers
		if ($entry == '.' || $entry == '..') {
			continue;
		}
		
		// Delete
		rmdirr($dirname."/".$entry);
	}
	$dir->close();
	return rmdir($dirname);
}

if(!Admin::checkPermission('canManageContent')) {
	General::ouputMessage('Fehler','Sie haben nicht gen&uuml;gend Rechte um diese Seite zu betrachten');
}else{
	
	if(isset($_GET['do']) && $_GET['do'] == "upload") {
		// Form-Data
		$formField[1]['fieldName'] = "Datei";
		$formField[1]['fieldType'] = Form::$TYPE_FILE;
		$formField[1]['fieldDefaultValue'] = "";
		$formField[1]['fieldCheck'] = false;
		
		$form = new Form();
		$form->initByData("Datei hochladen",true,"Upload!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			// Upload File
			if( file_exists("./content/".$_GET['folder']."/".$_FILES['Datei']['name']) ) {
				General::ouputMessage('Fehler','Es existiert schon eine Datei mit dem Namen.','form');
			}else{
				move_uploaded_file($_FILES['Datei']['tmp_name'],"./content/".$_GET['folder']."/".$_FILES['Datei']['name']);
				General::ouputMessage('Erfolgreich','Die Datei wurde erfolgreich hochgeladen.','form');
			}			
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	if(isset($_GET['file']) && isset($_GET['do']) && $_GET['do'] == "del") {
		require_once './kernel/formCheck.php';
		$form = new FormCheck("Bist du sicher?","L&ouml;schen");
		// Done?
		if($form->completeCheck()) {
			if($form->isChecked()) {
				// Delete navigation
				unlink("./content/".$_GET['file']);
			}
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	
	if(isset($_GET['do']) && $_GET['do'] == "newFolder") {
		// Form-Data
		$formField[0]['fieldName'] = "Name";
		$formField[0]['fieldType'] = Form::$TYPE_TEXT;
		$formField[0]['fieldDefaultValue'] = "";
		$formField[0]['fieldCheck'] = true;
		$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		$form = new Form();
		$form->initByData("Neuer Ordner!",true,"Erstellen!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			// Upload File
			if( file_exists("./content/".$_GET['folder']."/".$_POST['Name']) ) {
				General::ouputMessage('Fehler','Es existiert schon ein Ordner mit dem Namen.','form');
			}else{
				mkdir("./content/".$_GET['folder']."/".$_POST['Name'],0777);
				General::ouputMessage('Erfolgreich','Der Ordner wurde erfolgreich erstellt.','form');
			}			
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}

	if(isset($_GET['delFolder']) && isset($_GET['do']) && $_GET['do'] == "delFolder") {
		require_once './kernel/formCheck.php';
		$form = new FormCheck("Bist du sicher?","L&ouml;schen");
		// Done?
		if($form->completeCheck()) {
			if($form->isChecked()) {
				// Delete navigation
				rmdirr("./content/".$_GET['delFolder']);
			}
	}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	
	if(isset($_GET['file']) && isset($_GET['do']) && $_GET['do'] == "rename") {
		// Form-Data
		$formField[0]['fieldName'] = "Name";
		$formField[0]['fieldType'] = Form::$TYPE_TEXT;
		$formField[0]['fieldDefaultValue'] = basename($_GET['file']);
		$formField[0]['fieldCheck'] = true;
		$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		$form = new Form();
		$form->initByData("Datei/Ordner umbenennen!",true,"Umbennen!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			// Upload File
			if( file_exists("./content/".dirname($_GET['file'])."/".$_POST['Name']) ) {
				General::ouputMessage('Fehler','Es existiert schon ein Ordner mit dem Namen.','form');
			}else{
				rename("./content/".$_GET['file'],"./content/".dirname($_GET['file'])."/".$_POST['Name']);
				General::ouputMessage('Erfolgreich','Der Ordner/Datei wurde erfolgreich umbenannt.','form');
			}			
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	
	
	// Folder?
	$searchFolder = "";
	if(isset($_GET['folder'])) {
		General::getTpl()->assign('aktFolder',$_GET['folder']."/");
		$searchFolder = $_GET['folder']."/";
		
		//Pathstring
		$path = explode("/",trim($searchFolder,"/"));
		$outputPath = array();
	
		$outputPath["Root"] = "";
		foreach($path as $key=>$p) {
			$folderURL = "";
			$folder = "";
			for($i=0;$i<=$key;$i++) {
				$folderURL .= $path[$i]."/";
			}
			if($p != "") $outputPath[$p] = $folderURL;
		}
		
		General::getTpl()->assign('outputPath',$outputPath);
	}
	
	// Get Files
	$files = array();
	$folder = array();
	
	$DH = dir("./content/".$searchFolder);
	while ($extFile = $DH->read()) {
    	if (preg_match('/^..?$/', $extFile)) continue;
		if(is_dir("./content/".$searchFolder.$extFile)) {
			// Folder
			$folder[] = $extFile;
		}else{
			$files[] = $extFile;
		}
	}
    $DH->close();
	
	General::getTpl()->assign('files',$files);
	General::getTpl()->assign('folder',$folder);
	
	General::getTpl()->assign('body',
		General::getTpl()->fetch('../admin/contentManager.html'));
}
?>